
module.exports = ({
    uriName,
    payload,
}) => {
    return {
        mounted() {
            $(window).on('beforeunload', () => {
                if (_.isEmpty(this.difference)) {
                    return undefined;
                }
                return false;
          });
        },
        data(){
            return {
                uriName,
                original: _.clone(payload),
                payload: _.clone(payload),
                restClient: require('../restClient')({
                    'uriName': uriName,
                }),
            }
        },

        computed: {
            editing() {
                return Boolean(this.original.id)
            },
            creating() {
                return !this.editing
            },

            difference() {
                return _.reduce(this.payload, (result, value, key) => {
                    const cleanVal = _.isString(value) ? _.trim(value) : value;
                    let originVal = this.original[key]
                    if (_.isString(originVal)) {
                        originVal = _.trim(originVal)
                    }

                    if (_.isUndefined(originVal) && cleanVal === ''){
                        return result;
                    }
                    if (cleanVal === originVal) {
                        return result;
                    }

                    result[key] = cleanVal;
                    return result;
                }, {})
            },
            indexUrl() {
                return this.restClient.makeUrl()
            },
            showUrl() {
                return this.restClient.makeUrl(this.original.id)
            },
            createUrl() {
                return this.restClient.makeUrl() + '/create'
            },
        },
        methods: {
            log(...msg) {
                console.log(...msg)
            },
            handleResetField(field) {
                this.$set(this.payload, field, this.original[field])
            },
            handleResetAll() {
                this.payload = _.clone(this.original)
            },
            handleMSet(fields) {
                _.forEach(fields, (val, key) => {
                    this.$set(this.payload, key, val)
                })
            },
            handleDelete() {
                if (!this.payload.id) {
                    toastr.error('无法删除不存在数据')
                    return
                }
                this.restClient.destroy(this.payload.id).then(({data}) => {
                    setTimeout(() => {
                        window.location.href = this.restClient.makeUrl()
                    }, 1200)
                })
            },
            handleSubmit({data, stay}) {
                const sendData = data? data: this.difference
                if (_.isEmpty(sendData)) {
                    toastr.warning('没有要更新的字段')
                    return
                }
                if (this.editing) {
                    this.restClient.update(this.original.id, sendData).then(({data}) => {
                        console.log('update....')
                        this.original = _.clone(this.payload)
                        if (!stay) {
                            setTimeout(() => {
                                window.location.href = this.restClient.makeUrl(data.id)
                            }, 1200)
                        }
                    })
                } else {
                    this.restClient.store(sendData).then(({data}) => {
                        this.original = _.clone(this.payload)
                        setTimeout(() => {
                            window.location.href = this.restClient.makeUrl(data.id)
                        }, 1200)
                    })
                }
            },
        }
    }
}